package com.wang.users.dao.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.wang.common.dao.impl.BaseDaoImpl;
import com.wang.common.util.PageInfoUtil;
import com.wang.users.dao.IALogDao;
import com.wang.users.pojo.ALog;


/**
 * 日志dao实现类
 * @author Zjx
 *
 */
public class ALogDaoImpl extends BaseDaoImpl<ALog> implements IALogDao
{

	@Override
	public int saveOne(ALog log)
	{
		/* 准备一个List;条件;就是每一个?对应的值 */
		List<Object> paramsList = new ArrayList<Object>();
		/* sql语句 */
		StringBuilder sb = new StringBuilder();
		sb.append("insert into a_log (adminsId,logType,content,ip,status,createTime,updateTime,pubTime) ") ;
		sb.append(" values ");
		sb.append(" (?,?,?,?,?,?,?,?) ");
		
		paramsList.add(log.getAdminsId());
		paramsList.add(log.getLogType());
		paramsList.add(log.getContent());
		paramsList.add(log.getIp());
		paramsList.add(log.getStatus());
		paramsList.add(log.getCreateTime());
		paramsList.add(log.getUpdateTime());
		paramsList.add(log.getPubTime());
		
		/**
		 * 执行sql语句
		 */
		int res =  executeSql(sb.toString(),paramsList);
		/* 处理主键 */
		int id = (int) paramsList.get(paramsList.size() - 1);
		log.setId(id);
		return res ; 
	}

	@Override
	public int updateOne(ALog log)
	{
		/* 准备一个List;条件;就是每一个?对应的值 */
		List<Object> paramsList = new ArrayList<Object>();
		/* sql语句 */
		StringBuilder sb = new StringBuilder();
		sb.append("update a_log set ");
		sb.append(" adminsId=?,logType=?,content=?,ip=?,status=?,createTime=?,updateTime=?,pubTime=? ");
		sb.append(" where id = ? ");
		
		paramsList.add(log.getAdminsId());
		paramsList.add(log.getLogType());
		paramsList.add(log.getContent());
		paramsList.add(log.getIp());
		paramsList.add(log.getStatus());
		paramsList.add(log.getCreateTime());
		paramsList.add(log.getUpdateTime());
		paramsList.add(log.getPubTime());
		paramsList.add(log.getId());
		
		/* 执行sql语句 */
		return this.executeSql(sb.toString(), paramsList);
	}

	@Override
	public int deleteOne(Map<String, Object> condMap)
	{
		/* 准备一个List;条件;就是每一个?对应的值 */
		List<Object> paramsList = new ArrayList<Object>();
		/* sql语句 */
		StringBuilder sb = new StringBuilder();
		sb.append("delete from a_log ");
		if(condMap.get("id") != null && !"".equalsIgnoreCase(condMap.get("id") + ""))
		{
			sb.append(" where id = ? ");
			paramsList.add(condMap.get("id"));
		}else
		{
			/* 不传条件不能删除 */
			return 0 ; 
		}
		
		/* 执行sql语句 */
		return this.executeSql(sb.toString(), paramsList);
	}

	@Override
	public ALog findOne(Map<String, Object> condMap)
	{
		/* 准备一个List;条件;就是每一个?对应的值 */
		List<Object> paramsList = new ArrayList<Object>();
		/* sql语句 */
		StringBuilder sb = new StringBuilder();
		sb.append("select * from a_log ");
		if(condMap.get("id") != null && !"".equalsIgnoreCase(condMap.get("id") + ""))
		{
			sb.append(" where id = ? ");
			paramsList.add(condMap.get("id"));
		}else
		{
			return null ; 
		}
		
		return this.findOneSql(sb.toString(), paramsList,ALog.class);
	}

	@Override
	public List<ALog> findCondList(PageInfoUtil pageInfoUtil,Map<String, Object> condMap)
	{
		/* 准备一个List;条件;就是每一个?对应的值 */
		List<Object> paramsList = new ArrayList<Object>();
		/* sql语句 */
		StringBuilder sb = new StringBuilder();
		sb.append("select * from a_log where 1 = 1 ");
		
		/* 关键字 */
		if(condMap.get("keyword") != null && !"".equalsIgnoreCase(condMap.get("keyword") + ""))
		{
			sb.append(" and (email like ? or trueName like ? or phone like ? ) ");
			paramsList.add("%" + condMap.get("keyword") + "%");
			paramsList.add("%" + condMap.get("keyword") + "%");
			paramsList.add("%" + condMap.get("keyword") + "%");
		}
		
		/* 状态 */
		if(condMap.get("status") != null && !"".equalsIgnoreCase(condMap.get("status") + ""))
		{
			sb.append(" and status = ?  ");
			paramsList.add(condMap.get("status"));
		}
		
		/* adminsId */
		if(condMap.get("adminsId") != null && !"".equalsIgnoreCase(condMap.get("adminsId") + ""))
		{
			sb.append(" and adminsId = ?  ");
			paramsList.add(condMap.get("adminsId"));
		}
		
		/* logType */
		if(condMap.get("logType") != null && !"".equalsIgnoreCase(condMap.get("logType") + ""))
		{
			sb.append(" and logType = ?  ");
			paramsList.add(condMap.get("logType"));
		}
		
		/* 时间 */
		if(condMap.get("st") instanceof Date && condMap.get("ed") instanceof Date )
		{
			sb.append(" and lastLoginTime >= ? and lastLoginTime < ? ");
			paramsList.add(condMap.get("st"));
			paramsList.add(condMap.get("ed"));
		}
		
		/* 排序 */
		if("idDesc".equalsIgnoreCase(condMap.get("orderBy") + ""))
		{
			sb.append(" order by id desc ") ; 
		}else
		{
			sb.append(" order by lastLoginTime desc ");
		}
		
		if(pageInfoUtil != null)
		{
			return this.findListSql(pageInfoUtil, sb.toString(), paramsList,ALog.class);
		}
		return this.findListSql(sb.toString(), paramsList,ALog.class); 
	}

	@Override
	public List<Map<String,Object>> statList(PageInfoUtil pageInfoUtil,Map<String, Object> condMap)
	{
		/* 准备一个List;条件;就是每一个?对应的值 */
		List<Object> paramsList = new ArrayList<Object>();
		/* sql语句 */
		StringBuilder sb = new StringBuilder();
		if("dataStat".equalsIgnoreCase(condMap.get("operType") + ""))
		{
			if(pageInfoUtil != null)
			{
				sb.append("select DATE_FORMAT(createTime,'%Y-%m-%d') as createTime,count(*) as totalCount from a_log where 1 = 1 ");
				/* 加上logType */
				if(condMap.get("logType") != null && !"".equalsIgnoreCase(condMap.get("logType") + ""))
				{
					sb.append(" and logType = ?  ");
					paramsList.add(condMap.get("logType"));
				}
				
				/* 加上logType */
				if(condMap.get("st") instanceof Date && condMap.get("ed") instanceof Date)
				{
					sb.append(" and createTime >= ? and createTime < ? ");
					paramsList.add(condMap.get("st"));
					paramsList.add(condMap.get("ed"));
				}
				
				/* 分组 */
				sb.append("group by DATE_FORMAT(createTime,'%Y-%m-%d')");
				sb.append(" order by DATE_FORMAT(createTime,'%Y-%m-%d') asc ");
				return this.findListMapSql(pageInfoUtil, sb.toString(), paramsList);
			}else
			{
				sb.append("select DATE_FORMAT(createTime,'%Y-%m-%d') as createTime,count(*) as totalCount from a_log where 1 = 1 ");
				/* 加上logType */
				if(condMap.get("logType") != null && !"".equalsIgnoreCase(condMap.get("logType") + ""))
				{
					sb.append(" and logType = ?  ");
					paramsList.add(condMap.get("logType"));
				}
				
				/* 加上logType */
				if(condMap.get("st") instanceof Date && condMap.get("ed") instanceof Date)
				{
					sb.append(" and createTime >= ? and createTime < ? ");
					paramsList.add(condMap.get("st"));
					paramsList.add(condMap.get("ed"));
				}
				/* 分组 */
				sb.append("group by DATE_FORMAT(createTime,'%Y-%m-%d')");
				sb.append(" order by DATE_FORMAT(createTime,'%Y-%m-%d') asc ");
				return this.findListMapSql(sb.toString(), paramsList);
			}
		}
		return null;
	}
}
